﻿using FluentNHibernate.Mapping;
using g42.Entities;

// Mapping Beispiele: http://blogs.devart.com/dotconnect/nhibernate-mapping-samples.html

namespace g42.Repos
{
  class map
  {
    public class SampleEntMap : ClassMap<SampleEnt>
    {
      public SampleEntMap()
      {
        Id(x => x.Id);
        Map(x => x.Nr);
        Map(x => x.Bezeichnung);
        Table("Sample_Sa");
      }
    }

    public class MandantEntMap : ClassMap<MandantEnt>
    {
      public MandantEntMap()
      {
        Id(x => x.Id);
        Map(x => x.Nr);
        Map(x => x.Name);
        /*
        HasMany(x => x.PersonList) // 1:n zu PersonEnt
          .KeyColumn("Id") 
          .Inverse()              // Inverse on HasMany is an NHibernate term, and it means that the other end of the relationship is responsible for saving.
          .Cascade.All();         // HasManyToMany tells NHibernate to cascade events down to the entities in the collection (so when you save the Store, all the Products are saved too).
         */ 
        Table("Mandant_Md");
      }
    }

    public class AdresseEntMap : ClassMap<AdresseEnt>
    {
      public AdresseEntMap()
      {
        Id(x => x.Id);
        Map(x => x.Nr);
        Map(x => x.HausNr1);
        References(x => x.Strasse) // n:1 zu Strasse
          .Column("IdStr")
          .Not.Nullable();
        Table("Adresse_Adr");
      }
    }

    // Mapping für die Basisklasse aller Adressobj (Basis für Strasse, Ort, usw.)
    public class AdresseObjEntMap : ClassMap<BaseAdressObjEnt>
    {
      public AdresseObjEntMap()
      {
        // Mapping bei Vererbung: TableByClass (für jede Klasse gibt's eine Tabelle)
        // http://www.codeproject.com/Articles/232034/Inheritance-mapping-strategies-in-Fluent-Nhibernat
        // indicates that this class is the base one for the TPC inheritance strategy and that the values of its properties should be united with the values of derived classes
        UseUnionSubclassForInheritanceMapping();
        Id(x => x.Id);
        Map(x => x.Nr);
        Map(x => x.Name);
      }
    }

    // Mapping für die von AdresseObjEnt abgeleitete StrasseEnt
    public class StrasseEntMap : SubclassMap<StrasseEnt>
    {
      public StrasseEntMap()
      {
        // Schema("dbo");
        Map(x => x.Laenge);
        Table("Strasse_Str");
      }
    }



    public class PersonEntMap : ClassMap<PersonEnt>
    {
      public PersonEntMap()
      {
        Id(x => x.Id);
        Map(x => x.Nr);
        Map(x => x.Name).Not.Nullable().Length(100);
        Map(x => x.Vorname);
        References(x => x.Md) // n:1 zu MandantEnt
          .Column("IdMd");
        References(x => x.Adresse) // n:1 zu AdresseEnt
          .Column("IdAdr");
        Table("Person_Ps");
      }
    }

  }
}





